/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-07-26 17:45:30
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.bp.service.impl;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.bean.BpDeviceFacilityAttachmentDescDetailBean;
import com.joysuch.wwyt.bp.bean.BpDeviceFacilityAttachmentDescEditBean;
import com.joysuch.wwyt.bp.bean.BpDeviceFacilityAttachmentDescListBean;
import com.joysuch.wwyt.bp.bean.BpDeviceFacilityAttachmentDescListBean.SearchBpDeviceFacilityAttachmentDesc;
import com.joysuch.wwyt.bp.entity.BpDeviceFacilityAttachmentDesc;
import com.joysuch.wwyt.bp.entity.dsl.QBpDeviceFacilityAttachmentDesc;
import com.joysuch.wwyt.bp.repository.BpDeviceFacilityAttachmentDescDao;
import com.joysuch.wwyt.bp.service.BpDeviceFacilityAttachmentDescService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.service.BaseAttachmentRepositoryService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class BpDeviceFacilityAttachmentDescServiceImpl implements BpDeviceFacilityAttachmentDescService {

    @Autowired
    private BpDeviceFacilityAttachmentDescDao dao;
    @Autowired
    private BaseAttachmentRepositoryService baseAttachmentRepositoryService;

    @Override
    public BpDeviceFacilityAttachmentDesc add(BpDeviceFacilityAttachmentDescEditBean entity) {
        BpDeviceFacilityAttachmentDesc content = new BpDeviceFacilityAttachmentDesc();
        BeanUtils.copyProperties(entity, content, "id");
        if (entity.getAttachment() != null) {
            content.setAttachmentId(entity.getAttachment().getId());
        }
        dao.save(content);
        return content;
    }

    @Override
    public void update(BpDeviceFacilityAttachmentDescEditBean entity) {
        BpDeviceFacilityAttachmentDesc content = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity, content, "id");
        if (entity.getAttachment() != null) {
            content.setAttachmentId(entity.getAttachment().getId());
        } else {
            content.setAttachmentId(null);
        }
        dao.save(content);
    }

    @Override
    public Page<BpDeviceFacilityAttachmentDescListBean> findByPage(Pageable page,
                                                                   SearchBpDeviceFacilityAttachmentDesc condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<BpDeviceFacilityAttachmentDesc> data = predicate == null ? dao.findAll(page)
                : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data,
                new PageDataConvert<BpDeviceFacilityAttachmentDescListBean, BpDeviceFacilityAttachmentDesc>() {

                    @Override
                    public BpDeviceFacilityAttachmentDescListBean convert(BpDeviceFacilityAttachmentDesc f) {
                        BpDeviceFacilityAttachmentDescListBean bean = new BpDeviceFacilityAttachmentDescListBean();
                        BeanUtils.copyProperties(f, bean);
                        if (f.getAttachmentId() != null) {
                            bean.setAttachment(baseAttachmentRepositoryService.findByIdIntoBean(f.getAttachmentId()));
                        }
                        return bean;
                    }

                });
    }

    private Predicate prepareListSearchCondition(SearchBpDeviceFacilityAttachmentDesc condition) {
        Predicate p = null;
        QBpDeviceFacilityAttachmentDesc q = QBpDeviceFacilityAttachmentDesc.bpDeviceFacilityAttachmentDesc;
        if (condition.getRemark() != null) {
            p = ExpressionUtils.and(p, q.remark.eq(condition.getRemark()));
        }
        if (condition.getAttachmentId() != null) {
            p = ExpressionUtils.and(p, q.attachmentId.eq(condition.getAttachmentId()));
        }
        if (condition.getDeviceFacilityId() != null) {
            p = ExpressionUtils.and(p, q.deviceFacilityId.eq(condition.getDeviceFacilityId()));
        }
        return p;
    }

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     *
     * @param id 要删除的业务ID
     * @throws BussinessRestrictionsException 抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {
    }

    @Override
    public BpDeviceFacilityAttachmentDesc findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public BpDeviceFacilityAttachmentDescDetailBean getById(Long id) {
        BpDeviceFacilityAttachmentDesc entity = findById(id);
        if (entity == null) {
            return null;
        }
        BpDeviceFacilityAttachmentDescDetailBean bean = new BpDeviceFacilityAttachmentDescDetailBean();
        BeanUtils.copyProperties(entity, bean);
        return bean;
    }

}
